//
//  LoginViewController.m
//  test2
//
//  Created by 贺银银 on 16/3/24.
//  Copyright © 2016年 hyy. All rights reserved.
//

#import "LoginViewController.h"
#import "ChatListViewController.h"
#import <CDChatManager.h>
#import "SVProgressHUD.h"
@interface LoginViewController ()<UITextFieldDelegate>
- (IBAction)LoginAction:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UITextField *userNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *passWordTextField;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.userNameTextField.delegate = self;
    self.passWordTextField.delegate = self;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [textField resignFirstResponder];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated{
    
    self.navigationController.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBarHidden = YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)LoginAction:(UIButton *)sender {
    
    [AVUser logInWithUsernameInBackground:self.userNameTextField.text password:self.passWordTextField.text block:^(AVUser *user, NSError *error) {
        
        if (!error) {
            
            //显示登录成功
            [SVProgressHUD showSuccessWithStatus:@"登录成功"];

            [[CDChatManager manager]openWithClientId:self.userNameTextField.text callback:^(BOOL succeeded, NSError *error) {
                if (succeeded) {
                    
                    ChatListViewController * chatList = [[ChatListViewController alloc]init];
                    [self.navigationController pushViewController:chatList animated:YES];
                    [self dismissViewControllerAnimated:YES completion:nil];
                    
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"tiao" object:nil];
                }
                
            }];
        
        } else {

            [SVProgressHUD showErrorWithStatus:[error localizedDescription]];
            
        }

    }];
    
}

//- (void)login{
//    
//[AVOSCloud requestSmsCodeWithPhoneNumber:self.userNameTextField.text appName:nil operation:nil timeToLive:0 callback:^(BOOL succeeded, NSError *error) {
//    
//    }];
//    
//    [AVUser signUpOrLoginWithMobilePhoneNumberInBackground:self.userNameTextField.text smsCode:self.passWordTextField.text block:^(AVUser *user, NSError *error) {
//        
//        NSLog(@"%@",user.username);
//    }];
//    
//}

@end
